﻿// See https://aka.ms/new-console-template for more information
using LicenseDemo.Licenses;
using System.Diagnostics;
using System.Text;

{
    // 1.生成SM2密钥对
    var sm2KeyPairGenerator = Sm2KeyPairGenerator.Instance;
    sm2KeyPairGenerator.KeyPairInfo();
    var keyPair = sm2KeyPairGenerator.GenerateKeyPair();
    Console.WriteLine($"public => {keyPair.Public}");
    Console.WriteLine($"private => {keyPair.Private}");

    // 2.使用SM2签名数据 & 验证签名
    Sm2Assistant sm2Assistant = new();
    var data = Encoding.UTF8.GetBytes("hello");
    var signature = sm2Assistant.SignData(data, keyPair.Private);
    Console.WriteLine($"sign => {signature}");
    var verify = sm2Assistant.VerifyData(data, signature, keyPair.Public);
    Console.WriteLine($"verify => {verify}");

    // 3.创建许可证数据
    var licenseData = new LicenseData
    {
        AppName = "MyApp",
        AppVersion = "1.0.0",
        AuthCode = "1234567890",
        UserName = "John Doe",
        CompanyName = "ACME Corp",
        StartDate = DateTime.UtcNow,
        ExpirationDate = DateTime.UtcNow.AddDays(30),
        AllowedFeatures = ["PremiumFeature1", "PremiumFeature2"]
    };

    // 4. 实例化 LicenseAssistant
    LicenseAssistant licenseAssistant = new(keyPair);

    string targetDirectory = "lics";
    Directory.CreateDirectory(targetDirectory);
    Console.WriteLine("Project Folder Path: " + targetDirectory);

    string outputFilePath = Path.Combine(targetDirectory, "license.lic");

    // 4.1 生成许可证文件
    await licenseAssistant.GenerateLicenseFileAsync(licenseData, outputFilePath);

    // 4.2 验证许可证文件
    bool isValid = await licenseAssistant.VerifyLicenseFileAsync(outputFilePath);

    Console.WriteLine($"License is valid: {isValid}");

    // 5. 测试性能，生成 10000 个许可证文件，记录生成耗时
    int licenseCount = 10000;
    {
        Stopwatch stopwatch = Stopwatch.StartNew();
        await Task.Run(() =>
        {
            Parallel.For(0, licenseCount, async i =>
            {
                LicenseData licenseData = new()
                {
                    AppName = "MyApp",
                    AppVersion = "1.0.0",
                    AuthCode = "123456",
                    UserName = "John Doe",
                    CompanyName = "Acme Inc.",
                    StartDate = DateTimeOffset.Now,
                    ExpirationDate = DateTimeOffset.Now.AddDays(365),
                    AllowedFeatures = ["Feature1", "Feature2"]
                };

                string licenseFile = Path.Combine(targetDirectory, $"license1-{i}.lic");
                await licenseAssistant.GenerateLicenseFileAsync(licenseData, licenseFile);
            });
        });

        stopwatch.Stop();
        Console.WriteLine($"方法 1：使用 Parallel.For，Generated {licenseCount} license files in {stopwatch.ElapsedMilliseconds} ms");
    }

    {
        Stopwatch stopwatch2 = Stopwatch.StartNew();
        List<Task> tasks = [];
        for (int i = 0; i < licenseCount; i++)
        {
            int index = i; // 捕获循环变量
            tasks.Add(Task.Run(async () =>
            {
                LicenseData licenseData = new()
                {
                    AppName = "MyApp",
                    AppVersion = "1.0.0",
                    AuthCode = "123456",
                    UserName = "John Doe",
                    CompanyName = "Acme Inc.",
                    StartDate = DateTimeOffset.Now,
                    ExpirationDate = DateTimeOffset.Now.AddDays(365),
                    AllowedFeatures = ["Feature1", "Feature2"]
                };

                string licenseFile = Path.Combine(targetDirectory, $"license2-{index}.lic");
                await licenseAssistant.GenerateLicenseFileAsync(licenseData, licenseFile);
            }));
        }

        await Task.WhenAll(tasks);
        stopwatch2.Stop();
        Console.WriteLine($"方法 2：使用 Task.WhenAll，Generated {licenseCount} license files in {stopwatch2.ElapsedMilliseconds} ms");
    }
}
